package com.vgemv.jsutilitysdk.Crash;


import android.content.Context;
import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;


/**
 * 全局变量其实是很强的依赖,甚至是组合了.因为没有这个全局变量,就用不了.
 */

public class JsCrashManager  {
    /** Debug Log Tag */  
    public static final String TAG = "CrashHandler";
    /** CrashHandler实例 */  
    private static JsCrashManager _sharedInstance;
    /** 程序的Context对象 */  
    private Context mContext;  
    /** 系统默认的UncaughtException处理类 */  
    private UncaughtExceptionHandler mDefaultHandler;
    public JsCrashManagerCallback mCallback;

    /**
     * 初始化,注册Context对象, 获取系统默认的UncaughtException处理器, 设置该CrashHandler为程序的默认处理器
     * 保证只有一个CrashHandler实例
     */
    private JsCrashManager(Context context) {
        mContext = context;
        //默认的异常处理方法.
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();

        //设置自定义的处理回调.
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                if(mCallback != null) {
                    String info = getStackTraceAsString(throwable);
                    mCallback.onCrashOccured(throwable,info);
                }
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    Log.e(TAG, "Error : ", e);
                }
                mDefaultHandler.uncaughtException(thread, throwable);
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(10);
            }
        });
    }  
  
    /** 获取CrashHandler实例 ,单例模式 */  
    public static void initClass(Context context) {
        if (_sharedInstance == null) {
            _sharedInstance = new JsCrashManager(context);
        }
    }

    public static JsCrashManager sharedInstance() {
        return _sharedInstance;
    }


//    private void toastExeptionInfo(Throwable e) {
//
//        final String msg = e.getLocalizedMessage();
//        final String str_stackTraceInfo = getStackTraceAsString(e);
//        //  e.printStackTrace();
//        // 使用Toast来显示异常信息
//        new Thread() {
//            @Override
//            public void run() {
//                Log.v("","toastException");
//                // Toast 显示需要出现在一个线程的消息队列中,当前线程需要被sleep.
//                Looper.prepare();
//                Toast.makeText(mContext, "程序出错啦:" + msg, Toast.LENGTH_LONG).show();
//                JsLogManager.logc_d("","SystemCrash:\n%s",str_stackTraceInfo);
//                Looper.loop();
//            }
//        }.start();
//    }

    private String getStackTraceAsString(Throwable e) {
        // StringWriter将包含堆栈信息
        StringWriter stringWriter = new StringWriter();
        //必须将StringWriter封装成PrintWriter对象，
        //以满足printStackTrace的要求
        PrintWriter printWriter = new PrintWriter(stringWriter);
        //获取堆栈信息
        e.printStackTrace(printWriter);
        //转换成String，并返回该String
        StringBuffer error = stringWriter.getBuffer();
        return error.toString();
    }
}  